package com.shopcart.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.mdx.mobile.commons.verfy.Md5;
import com.shopcart.domain.Store;
import com.vsd.service.StoreService;

@Controller
@RequestMapping("store")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class StoreController extends CoreAdminController{
	@Autowired
	private StoreService storeService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String storeList(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,HttpServletRequest request){
		
		Page<Store> p = null;
		if(StringUtils.isBlank(search)){
			p= storeService.findAll(new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
		}else{
			p = storeService.findByNameLike("%"+search+"%", new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
		}
		
		List<Store> list = new ArrayList<>();
		for (Store store : p) {
			list.add(store);
		}
		return JSONUtils.serialize(new DataTable(draw+1,Integer.parseInt(p.getTotalElements()+""), p.getContent()));
	}
	
	@RequestMapping("/listByArea")
	@ResponseBody
	public String listByArea(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
		Page<Store> p = null;
		if(StringUtils.isBlank(search)){
			p= storeService.findAreaCode(areaCode,new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
		}else{
			p = storeService.findAreaCodeAndNameLike(areaCode,search, new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
		}
		
		return JSONUtils.serialize(new DataTable(draw+1,Integer.parseInt(p.getTotalElements()+""), p.getContent()));
	}
	

	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id,HttpServletRequest request){
		Store s = storeService.findById(id);
		return JSONUtils.serialize(s);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(Store store) throws Exception{
		if (StringUtils.isBlank(store.getId())){
			store.setId(StringUtil.getUUID());
    		store.setCreateTime(new Date());
    		store.setRoleCode("store");
    		store.setPassword(Md5.md5(store.getPassword()));
    		store.setClosed(1);
    		store.setIsOnline(1);
    		store.setIsRecommend(0);
    		store.setStoreType(1);
    		store.setWineLeft(store.getWineUnit());
    		storeService.save(store);
    	}else{
    		store.setAccount(null);
    		store.setAreaCode(null);
    		storeService.update(store);
    	}
       	return JSONUtils.serialize(new Ret(0));
	}
	
}
